Page({
  data: {
    autoplay: true,
    interval: 2000,
    duration: 500,
    imgUrls:[
      "/images/shouye_04.png",
      "/images/shouye_05.png",
      "/images/shouye_06.png"
    ],
    images_detail:[],
    // cart_button_flag:true,
    // goods:{"goods_id":"111","goods_selling_price":32.3,
    // "goods_name":"商品名称1","goods_quantity":34,"goods_selling_count":12,
    // "is_GGCT":true,"is_seven_day":true,"is_guarantee":true,"is_return_goods":true},
  },
  onLoad(query) {
    console.log(query);
    this.init(query.id);
  },

  //拨打客服电话
  call(){
    my.alert({
      title: '拨打客服电话：400-860-3111', // alert 框的标题
      success: (res) => {
        my.makePhoneCall({
          number: '13883632536', // 电话号码
          success: (res) => {
            
          },
        });
      },
      fail:(res)=>{

      }
    });
  },

  //读取商品信息
  init(id){
        var app = getApp()
      var url = app.globadData.apiUrlHeader+'get-good'
      console.log(id);
      // var id = this.data.goods.goods_id
      my.httpRequest({
      url: url, // 目标服务器 url
      method:'Post',
      headers:{'Content-Type':'Application/json'},
      data:JSON.stringify({
          "goods_id":id,
          'access_token':app.globadData.access_token
        }),
        dataType:'json',
        success: (res) => {
          // console.log(res.data.data.images_detail[0].url);
          this.setData({
            goods : res.data.data,
            imgUrls : res.data.data.images,
            images_detail:res.data.data.images_detail
          })
        // if(res.data.status==0)
        // {
          
          // my.showToast({
          //    type: 'success',
          //    content: res.data.message,
          //    duration: 3000,
          //             });
        // }
      },
    });
  },
 
  goCartTap(){
     my.redirectTo({
       url: '/pages/main/cart/cart', // 需要跳转的应用内非 tabBar 的页面的路径，路径后可以带参数。参数与路径之间使用
     });
  },

  //收藏
  async favoriateTap(){
      var app = getApp()
      var i = await app.getUserInfo();
      var token = await app.login(i.data.auth_code);
      var url = app.globadData.apiUrlHeader+'add-favorite'
      var id = this.data.goods.id
      console.log(id);
      console.log(token);
      my.httpRequest({
      url: url, // 目标服务器 url
      method:'Post',
      headers:{'Content-Type':'Application/json'},
      data:JSON.stringify({
          "goods_id":id,
          'access_token':token          
        }),
        dataType:'json',
        success: (res) => {
          console.log(res);
        // if(res.data.status==0)
        // {
          
          my.showToast({
             type: 'success',
             content: res.data.message,
             duration: 3000,
                      });
        // }
      },
    });
  },

  //立即购买
  buyNowTap(){
    this.login();
    
      var id = this.data.goods.id
     
    
    my.navigateTo({
      url: `/pages/main/settleAccount/settleAccount?goods_id=${this.data.goods.id}`, // 需要跳转的 tabBar 页面的路径（需在 app.json 的 tabBar 字段定义的页面），路径后不能带参数
    });
  },

     async login(){
      var app = getApp()
      console.log('1');
      var i = await app.getUserInfo();
      // console.log('2');
      if(i==-1){
        console.log('拒绝授权！');
        
       var token='';
        my.getStorage({
          key: 'token', // 缓存数据的 key
          success: (res) => {
            console.log(res);
            token = res;
          },
        });
        if(token != '')
        token = await app.touristLogin(i);
      // console.log(j);
      app.globadData.access_token = token;
      app.globadData.isTourist = 'ture';
      
      //将游客token存储到本地
      my.setStorage({
        key: 'token', // 缓存数据的 key
        data: token, // 要缓存的数据
        success: (res) => {
          
        },
      });
      // this.add();
      // this.getNewGoods();
        return;
      }
      console.log(i);
      console.log(i.data.auth_code);
      console.log('2');
      app.globadData.userInfo.nickName = i.data.nickName;
      console.log('3');
      app.globadData.userInfo.avatar = i.data.avatar;
      var j = await app.login(i.data.auth_code);
      console.log(j);
      app.globadData.access_token = j;
      // this.add();
      // this.getNewGoods();
  },


  //添加购物车
  addCartTap(){
    this.login();
      var app = getApp()
      var url = app.globadData.apiUrlHeader+'add-cart'
      var id = this.data.goods.id
      var quantity = 1
      console.log(id+':'+quantity);
      my.httpRequest({
      url: url, // 目标服务器 url
      method:'Post',
      headers:{'Content-Type':'Application/json'},
      data:JSON.stringify({
        // "goods_ids":[{"goods_id":"111","quantity":"2"},{"goods_id":"222","quantity":"23"}]
          'access_token':app.globadData.access_token,          
        "goods_ids":[{"goods_id":id,"quantity":quantity}]
        }),
        dataType:'json',
        success: (res) => {
          // console.log(res);
        // if(res.data.status==0)
        // {
          
          my.showToast({
             type: 'success',
             content: res.data.message,
             duration: 3000,
                      });
        // }
      },
    });
  }
});
